﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectPortfolioChangeActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {
                this.Prompt("请登录", false);
                this.Context.Send("Login",true);
                // response.Redirect("Account", "Login");

            }
            var subId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", request.Model, "Select"), 0);
            var sub = DataFactory.Instance().Subject(subId);
            var meta = new WebMeta();
            if (sub.project_id.HasValue)
            {
                meta.Put("Project", sub.project_id);
                var member = DataFactory.Instance().ProjectMember(sub.project_id.Value, user.Id.Value);

                if (member != null)
                {
                    switch (member.AuthType)
                    {
                        case WebAuthType.Admin:
                        case WebAuthType.User:
                            break;
                        default:
                            this.Prompt("您未有编辑此图文的权限");
                            break;
                    }

                }
                else
                {
                    this.Prompt("您未有编辑此图文的权限");
                }
                var item = request.SendValues?.Get("Item");
                if (String.IsNullOrEmpty(item) == false)
                {
                    meta["Item"] = item;
                }
            }
            var sid = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Portfolio", request.Model, "Portfolio", meta), 0);
            var portfolio = DataFactory.Instance().Portfolio(sid);


            DataFactory.Instance().Put(new Subject
            {
                Id = subId,
                portfolio_id = sid,
                project_item_id = portfolio.project_item_id,
                project_id = portfolio.project_id
            });

            this.Context.Send(new UMC.Web.WebMeta().Put("type", "Subject.Portfolio.Change").Put("Id", portfolio.Id).Put("Sub", subId), false);
        }

    }
}